const router = require('koa-router')()
const svgCaptcha = require('svg-captcha')
const { resBody } = require("../../utils")
// 验证码获取
router.post('/getCode', async(ctx) => {
    //运算验证码
    // var captcha = svgCaptcha.createMathExpr({
    //     size:4,
    //     fontSize:50,
    //     width:100,
    //     height:40,
    //     background:"#cc9966"
    // });
    let {
        type = 'reg'
    } = ctx.request.body;

    //数字验证码
    try {
        var captcha = svgCaptcha.create({
            size:4,
            fontSize:45,
            width:100,
            height:40,
            ignoreChars: '0o1i',
            background:"#cc9966"
        });
        let captchaText = captcha.text.toLowerCase()
        // 保存验证码
        switch (type) {
            case 'reg':
                ctx.session.verifyRegCode = captchaText;
                break;
            case 'login':
                ctx.session.verifyLoginCode = captchaText;
                break;
            case 'adminLogin':
                ctx.session.verifyAdminCode = captchaText;
                break;
            default:
                break;
        }
        // 设置响应头
        ctx.set('Content-Type', 'image/svg+xml')
        //返回生成的验证码
        ctx.body = {
            ...resBody.success,
            data: captcha.data
        };
    } catch (error) {
        throw ({
            ...resBody.createFail,
            msg: '验证码获取失败！'
        });     
    }
});
module.exports = router
